const wsEnv = {
    "app_name": "douyin_web",
    "version_code": "180800",
    "webcast_sdk_version": "1.0.14-beta.0",
    "update_version_code": "1.0.14-beta.0",
    "compress": "gzip",
    "device_platform": "web",
    "cookie_enabled": true,
    "screen_width": 1920,
    "screen_height": 1080,
    "browser_language": "zh-CN",
    "browser_platform": "Win32",
    "browser_name": "Mozilla",
    "browser_version": "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
    "browser_online": true,
    "tz_name": "Asia/Shanghai",
    "host": "https://live.douyin.com",
    "aid": "6383",
    "live_id": 1,
    "did_rule": 3,
    "endpoint": "live_pc",
    "support_wrds": 1,
    "im_path": "/webcast/im/fetch/",
    "identity": "audience",
    "need_persist_msg_count": "15",
    "insert_task_id": "",
    "live_reason": "",
    // "room_id": "7408022675794217791",
    "heartbeatDuration": "0"
}
const wsParamProp = [
    {
        "param_name": "live_id",
        "param_type": "string"
    },
    {
        "param_name": "aid",
        "param_type": "string"
    },
    {
        "param_name": "version_code",
        "param_type": "string"
    },
    {
        "param_name": "webcast_sdk_version",
        "param_type": "string"
    },
    {
        "param_name": "room_id",
        "param_type": "string"
    },
    {
        "param_name": "sub_room_id",
        "param_type": "string"
    },
    {
        "param_name": "sub_channel_id",
        "param_type": "string"
    },
    {
        "param_name": "did_rule",
        "param_type": "string"
    },
    {
        "param_name": "user_unique_id",
        "param_type": "string"
    },
    {
        "param_name": "device_platform",
        "param_type": "string"
    },
    {
        "param_name": "device_type",
        "param_type": "string"
    },
    {
        "param_name": "ac",
        "param_type": "string"
    },
    {
        "param_name": "identity",
        "param_type": "string"
    }
]
import * as byted_acrawler from './webmssdk.es5.cjs'
// Calculate a rolling hash.
import { createHash } from 'node:crypto';

export const signXbogus = (roomId, user_unique_id) => {
    // console.debug("roomId=",roomId)
    wsEnv.room_id = roomId;
    wsEnv.user_unique_id = user_unique_id;
    const hash = createHash('MD5');
    var n, sdk, i;
    let o = "";
    for (let { param_name: r } of wsParamProp)
        o += `,${r}=${null != (n = wsEnv[r]) ? n : ""}`;
    hash.update(o.substring(1));
    console.debug(o.substring(1))
    let a = hash.digest('hex')
        , A = {};
    console.debug(a)

    // console.log("编码" + a)
    var sign = byted_acrawler.frontierSign({
        "X-MS-STUB": a
    })
    console.debug(sign)
    return sign['X-Bogus']
    // TODO 有时间理解下这段代码的语法,copy from douyin web
    // return byted_acrawler && (A = null == (sdk = byted_acrawler) ? void 0 : sdk.frontierSign({
    //     "X-MS-STUB": a
    // })),
    // {
    //     signature: null != (i = A["X-Bogus"]) ? i : ""
    // }
}

export const signAbogus = (url, params) => {



    return "TODO"
}

